package com.example.demo.service.serviceImpl;

import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.SignIn;
import com.example.demo.entity.requestJson.ListSignInDetailRequest;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.SignInMapper;
import com.example.demo.mapper.WalletMapper;
import com.example.demo.service.SignInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import java.time.LocalDate;
import java.util.List;
/**
 * 签到表(SignIn)Service层
 *
 * @author 杨丽辉
 * @since 2021-03-22 11:38:40
 */
@Service
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class SignInServiceImpl implements SignInService {
        @Autowired
        private SignInMapper signInMapper;
        @Autowired
        private WalletMapper walletMapper;
        private Jedis jedis = new Jedis();
 /**
     * 通过ID和其他条件查询单条数据
     *
     * @param signIn 实例对象
     * @return 实例对象
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
     public SignIn querySignInLimit1(SignIn signIn){
     return  signInMapper.querySignInLimit1( signIn);
     }
     
 /**
     * 查询指定行数据
     * @param signIn 实例对象
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public   List<SignIn> queryAllSignInByLimit( SignIn signIn, Integer offset,  Integer limit){
   return signInMapper.queryAllSignInByLimit(signIn, offset,limit);
  }

  /**
     * 查询数据条数
     * @return 条数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public int countAllSignIn(SignIn signIn){
   return signInMapper.countAllSignIn( signIn);
   }
   /**
     * 通过实体作为筛选条件查询
     *
     * @param signIn 实例对象
     * @return 对象列表
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public List<SignIn> queryAllSignIn(SignIn signIn){
   return signInMapper.queryAllSignIn(signIn);
   }
  /**
     * 新增数据
     *
     * @param signIn 实例对象
     * @return 影响行数
     */
     @Override
   @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public int insertSignIn(SignIn signIn){
    return  signInMapper.insertSignIn( signIn);
    }
  /**
     * 修改数据
     *
     * @param signIn 实例对象
     * @return 影响行数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
     public  int updateSignIn(SignIn signIn){
         return signInMapper.updateSignIn( signIn);
     }

    /**
     * 通过主键删除数据
     * @param signId 主键
     * @return 影响行数
     */
      @Override
  @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public  int deleteSignInById(String signId){
   return  signInMapper.deleteSignInById( signId);
   }
     /**
     * 通过主键列表删除，列表长度不能为0
     * @param list 集合
     * @return 条数
     */
      @Override
  @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public  int deleteSignInByIds( List<String> list){
    return  signInMapper.deleteSignInByIds(  list);
  }
    
       /**
     * 通过主键修改实体列表，列表长度不能为0，注意：当实体属性为null时，对应的列也会别更新为null
     * @param list 集合
     * @return 条数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public int updateSignInBatch( List<SignIn> list){
   return  signInMapper.updateSignInBatch(list);
   }
    /**
     * 批量其他列,除主键以外的列，列表长度不能为0,字段是空的就置为null
     * @param list 集合
     * @return 条数
     */
      @Override
  @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
 public   int insertSignInBatch( List<SignIn> list){
 return signInMapper.insertSignInBatch( list);
 }
      /**
     * 通过实体非空属性删除
     * @param signIn  实体
     * @return 条数
     */
        @Override
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public   int deleteSignInByEntity( SignIn signIn){
  return  signInMapper.deleteSignInByEntity(  signIn);
  }
    /**
     * 通过实体不为空的属性作为筛选条件查询列表
     * @param signIn  实体
     * @return list
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public  List<SignIn> listSignInByEntity(SignIn signIn){
   return signInMapper.listSignInByEntity( signIn);
   }

 /**
     * 新增实体属性不为null的列
     * @param signIn  实体
     * @return 条数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public  int insertSignInSelect( SignIn signIn){
      return signInMapper.insertSignInSelect( signIn);

   }
   
      /**
     * 批量新增新增实体属性不为null的列
     * @param list  实体
     * @return 条数
     */
        @Override
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public  int insertSignInListSelect( List<SignIn> list){

     return signInMapper.insertSignInListSelect( list);
      }
     /**
     * 通过实体不为空的属性作为筛选条件查询单个
     * @param signIn  实体
     * @return 单个
     */
     @Override
   @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public  SignIn getSignInByEntity(SignIn signIn){
     return  signInMapper.getSignInByEntity(signIn);
    }

    /**
     * 是否签到
     */
    @Override
    public BackResponseEntity<Object> checkSignIn(ListSignInDetailRequest listSignInDetailRequest) {
        LocalDate today = LocalDate.now();

        SignIn signIn = new SignIn();
        signIn.setUserId(listSignInDetailRequest.getUserId());
        SignIn res = new SignIn();
        res = signInMapper.querySignInLimit1(signIn);

        if (res.getSignInDate().isEqual(listSignInDetailRequest.getDate())) {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1052, "今天已签到");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1053, "今日未签到");
        }
    }

    /*@Override
    public BackResponseEntity<Object> postSignIn(ListSignInDetailRequest listSignInDetailRequest) {

    }*/

}

